package com.mc2.docapp.disabilityassessmenttool;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mc2.docapp.disabilityassessmenttool.Amputation.AM_CustDataType;
import com.mc2.docapp.disabilityassessmenttool.Amputation.Class_Amputation;
import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.utils.MailerObject;
import com.mc2.docapp.disabilityassessmenttool.utils.SendMailTLS;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;


public class AmputationLowerLimb extends AppCompatActivity {
    private static final int id = Activities.LowerAmputation;
    EditText amputationLowerLimbLEFT ;
    EditText amputationLowerLimbRIGHT ;
    private Realm realm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amputation_lower_limb);

        amputationLowerLimbLEFT = (EditText) findViewById(R.id.amputationLowerLimbLEFT);
        amputationLowerLimbRIGHT = (EditText) findViewById(R.id.amputationLowerLimbRIGHT);

        Class_Amputation class_amputation = new Class_Amputation();

        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        initRealmDB();
        try {
            //mapUI((realm.where(Class_Amputation.class)/*.equalTo("id", id)*/.findAll()).get(0));
            System.out.println();
        } catch (Exception e) {
            System.out.println(e.getCause() + " " + e.getMessage());
        }
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewPatient.finalDto.getAmputation().setLower_amputation(new AM_CustDataType(Double.parseDouble(IfNull(amputationLowerLimbRIGHT.getText().toString(), "0")),
                        Double.parseDouble(IfNull(amputationLowerLimbLEFT.getText().toString(), "0")), 0d));
                class_amputation.setLower_amputation(new AM_CustDataType(Double.parseDouble(IfNull(amputationLowerLimbRIGHT.getText().toString(), "0")),
                        Double.parseDouble(IfNull(amputationLowerLimbLEFT.getText().toString(), "0")), 0d));
                calSubTotal();


                realm.beginTransaction();
                realm.delete(Class_Amputation.class);
                realm.insert(class_amputation);
                realm.commitTransaction();
                try {
                    String str = new ObjectMapper().writeValueAsString(class_amputation).toString();
//                    SendMailTLS.getMailer().sendMail(new MailerObject("saurab.hbhumkar@gmail.com", "MC2 Docapp", "str"));
                } catch (Exception e) {
                    System.out.println();
                }



                Intent intent = new Intent(AmputationLowerLimb.this, router(AmputationLowerLimb.class));
                startActivity(intent);
            }
        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });
        sanitizeInputs();
    }

    private void mapUI(Class_Amputation oldData) {
        amputationLowerLimbLEFT.setText(String.valueOf((long) (oldData.getLower_amputation().getActual_left())));
        amputationLowerLimbRIGHT.setText(String.valueOf((long) (oldData.getLower_amputation().getActual_right())));
    }

    private void calSubTotal() {
        double a = NewPatient.finalDto.getAmputation().getUpper_amputaion().getActual_left();
        double b =  NewPatient.finalDto.getAmputation().getUpper_amputaion().getActual_right();
        double c = NewPatient.finalDto.getAmputation().getLower_amputation().getActual_left();
        double d = NewPatient.finalDto.getAmputation().getLower_amputation().getActual_right();

        double step1;
        double step2;
        double step3;

        step1 = calByFormula(a,b);
        step2 = calByFormula(step1,c);
        step3 = calByFormula(step2,d);

        NewPatient.finalDto.getAmputation().setSubtotal(step3);
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(id + ".realm")
                .build();
        realm = Realm.getInstance(config);
    }

    public String IfNull(String value, String defaultValue) {
        if(value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return  value;
        }
    }

    private double calByFormula(double a, double b) {
        if (a < b) {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        return (a + (((90 - a) * b) / 90.0));
    }

    public void sanitizeInputs() {
        amputationLowerLimbLEFT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "100")});
        amputationLowerLimbRIGHT.setFilters(new InputFilter[]{new InputFilterMinMax("0", "100")});
    }
}